package com.pengliu.common.manager;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.pengliu.common.exception.ServiceException;
import com.pengliu.common.exception.ExceptionEnums;
import com.pengliu.common.mybatis.mapper.Mapper;
import com.pengliu.common.mybatis.model.Model;
import com.pengliu.common.mybatis.model.ModelExample;
import com.pengliu.common.mybatis.page.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * user: pengliu
 * date: 2017年10月17日
 * desc:
 * @author pengliu
 */
public abstract class ManagerImpl<M extends Model, E extends ModelExample, ID> implements Manager<M, E, ID>,Serializable {

	protected final Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * user: pengliu
	 * date: 2017年10月17日
	 * desc: 获取mapper注入对象
	 * @return
	 * @author pengliu
	 */
	public abstract Mapper<M, E, ID> getMapper();

	/**
	 * 按条数计算
	 */
	@Override
	final public int countByExample(E example) {
		return getMapper().countByExample(example);
	}

	/**
	 * 根据条件删除数据
	 */
	@Override
	final public int deleteByExample(E example) {
		return getMapper().deleteByExample(example);
	}

	/**
	 * 根据主键删除数据
	 */
	@Override
	final public int deleteByPrimaryKey(ID id) {
		return getMapper().deleteByPrimaryKey(id);
	}

	/**
	 * 新增数据
	 */
	@Override
	final public int insert(M model) {
		int flag = 0;
		try {
			flag = getMapper().insert(model);
			if (flag == 0) {
				// logger.info("新增数据", model);
				throw new ServiceException(ExceptionEnums.Error);
			}
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	@Override
	final public M insertObject(M model) {
		int flag = 0;
		try {
			flag = getMapper().insert(model);
			if (flag == 0) {
				// logger.info("新增数据", model);
				throw new ServiceException(ExceptionEnums.Error);
			}
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		return flag == 0 ? null:model;
	}

	/**
	 * 按条件查询
	 */
	@Override
	final public List<M> selectByExample(E example) {
		List<M> list = getMapper().selectByExample(example);
		if (list == null) {
			list = Collections.emptyList();
		}
		return list;
	}

	/**
	 * 根据主键查询数据
	 */
	@Override
	final public M selectByPrimaryKey(ID id) {
		return getMapper().selectByPrimaryKey(id);
	}

	/**
	 * 根据条件查询1条数据
	 */
	@Override
	final public M selectOneByExample(E example) {
		example.setLimit(1);
		example.setOffset(0);
		List<M> list = selectByExample(example);
		if(!list.isEmpty()) {
			return list.get(list.size()-1);
		}
		return null;
	}

	/**
	 * 根据条件查询分页
	 */
	@Override
	final public Page<M> selectPageByExample(E example, Page<M> page) {
		example.setLimit(page.getPageSize());
		example.setOffset(page.getOffset());
		List<M> content = selectByExample(example);
		int total = countByExample(example);
		page.setTotal(total);
		page.setResult(content);
		return page;
	}

	@Override
	final public PageInfo<M> selectPageInfo(E example) {
		PageHelper.startPage(example.getPiCurrent(), example.getPiLimit());
		List<M> content = getMapper().selectByExample(example);
		PageInfo<M> pageInfo = new PageInfo<M>(content);
		return pageInfo;
	}

	/**
	 * 按条件更新
	 */
	@Override
	final public int updateByExampleSelective(M model, E example) {
		return getMapper().updateByExampleSelective(model, example);
	}

	/**
	 * 按主键更新
	 */
	@Override
	final public int updateByPrimaryKeySelective(M model) {
		return getMapper().updateByPrimaryKeySelective(model);
	}
}
